package com.huaruan.youdi.views.activitys;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaruan.youdi.R;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpUtils;
import com.huaruan.youdi.models.LoginModel;
import com.huaruan.youdi.utils.AppManager;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.PreferenceUtil;
import com.huaruan.youdi.utils.StringUtils;

import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.tv_forget_password)
    TextView tv_forget_password;
    @BindView(R.id.iv_password_visible)
    ImageView iv_password_visible;
    @BindView(R.id.tv_login)
    TextView tv_login;

    private String userPhone;
    private String userPassword;
    boolean isPasswordVisible;

    @Override
    public int IGetContentViewResId() {
        return R.layout.activity_login;
    }

    @Override
    public void IFindViews() {
        iv_password_visible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPasswordVisible();
            }
        });
        tv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        tv_forget_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppUtils.jump(IGetContext(), RegisterActivity.class);
            }
        });
    }

    @Override
    public void IInitData() {
        setPasswordVisible();
    }

    private void setPasswordVisible() {
        isPasswordVisible = !isPasswordVisible;
        if (isPasswordVisible) {
            iv_password_visible.setImageResource(R.mipmap.icon_password_visible);
            et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            iv_password_visible.setImageResource(R.mipmap.icon_password_invisible);
            et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    private void login() {
        SortedMap<String, String> mSortedMap = new TreeMap<>();
        userPhone = et_phone.getText().toString();
        userPassword = et_password.getText().toString();
        if (StringUtils.isEmpty(userPhone)) {
            AppUtils.toastShort(IGetContext(), "请输入您的手机号");
            return;
        }
        if (userPhone.length() != 11) {
            AppUtils.toastShort(IGetContext(), "请输入正确的手机号");
            return;
        }
        if (StringUtils.isEmpty(userPassword)) {
            AppUtils.toastShort(IGetContext(), "请输入您的密码");
            return;
        }

        mSortedMap.put("username", userPhone);
        mSortedMap.put("password", userPassword);
        mSortedMap.put("type", "2");
        new HttpUtils.Builder(IGetContext())
                .postMethod()
                .url(Constant.LOGIN)
                .params(mSortedMap)
                .modelClass(LoginModel.class)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object object) {
                        LoginModel loginModel = (LoginModel) object;
                        PreferenceUtil.putString(PreferenceUtil.PreferenceKeys.TOKEN, loginModel.authorization);
                        PreferenceUtil.putString(PreferenceUtil.PreferenceKeys.PHONE, loginModel.username);
                        PreferenceUtil.putString(PreferenceUtil.PreferenceKeys.UID, loginModel.userId);
                        AppUtils.jump(IGetContext(), MainActivity.class);
                        finish();
                    }
                }).start();
    }

//    private void login() {
//        userPhone = et_phone.getText().toString();
//        userPassword = et_password.getText().toString();
//        if (StringUtils.isEmpty(userPhone)) {
//            AppUtils.toastShort(IGetContext(), "请输入您的手机号");
//            return;
//        }
//        if (userPhone.length() != 11) {
//            AppUtils.toastShort(IGetContext(), "请输入正确的手机号");
//            return;
//        }
//        if (StringUtils.isEmpty(userPassword)) {
//            AppUtils.toastShort(IGetContext(), "请输入您的密码");
//            return;
//        }
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("username", userPhone);
//        map.put("password", userPassword);
//        map.put("type", "2");
//        HttpSubscribe.login(map, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
//            @Override
//            public void onSuccess(String result) {
//                LoginModel model = (LoginModel) GsonUtil.parseResponse(result, LoginModel.class);
//                PreferenceUtil.putString(PreferenceUtil.PreferenceKeys.TOKEN, model.authorization);
//                PreferenceUtil.putString(PreferenceUtil.PreferenceKeys.PHONE, model.username);
//                PreferenceUtil.putString(PreferenceUtil.PreferenceKeys.UID, model.userId);
//                AppUtils.jump(IGetContext(), MainActivity.class);
//                finish();
//            }
//
//            @Override
//            public void onFault(String errorMsg) {
//                AppUtils.toastLong(IGetContext(), errorMsg);
//            }
//        }));
//    }

    long time = 0;

    @Override
    public void onBackPressed() {
        long t = System.currentTimeMillis();
        if (t - time <= 2 * 1000) {
            AppManager.getAppManager().AppExit();
        } else {
            time = System.currentTimeMillis();
            AppUtils.toastShort(IGetContext(), "再按一次退出24优递");
        }
    }

}
